package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.*;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author xiaycbstart
 * @create 2019 12 30 16:10
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);
        if (user == null) {
            return null;
        }
        Integer userId = user.getId();
        Set<Role> roles = roleDao.findByUserId(userId);
        if (roles != null && roles.size() > 0) {
            for (Role role : roles) {
                Integer roleId = role.getId();
                Set<Permission> permissions = permissionDao.findByRoleId(roleId);
              /*  LinkedHashSet<Menu> menuList = menuDao.findMenuListByRoleId(roleId);
                if (menuList != null) {
                    for (Menu menu : menuList) {
                        List<Menu> list = menuDao.findMenuListByMenuId(menu.getId());
                        menu.setChildren(list);
                    }
                }*/
                if (permissions != null && permissions.size() > 0) {
                    role.setPermissions(permissions);
                }
               /* if (menuList != null && menuList.size() > 0) {
                    role.setMenus(menuList);
                }*/
            }
            user.setRoles(roles);
        }
        return user;
    }

    @Override
    public LinkedHashSet<Menu> findMenuListByUsername(String username) {
        User user = userDao.findByUsername(username);
        if (user == null) {
            return null;
        }
        Integer userId = user.getId();
        Set<Role> roles = roleDao.findByUserId(userId);
        LinkedHashSet<Menu> menuListFin = new LinkedHashSet<>();
        if (roles != null && roles.size() > 0) {
            for (Role role : roles) {
                Integer roleId = role.getId();
                LinkedHashSet<Menu> menuList = menuDao.findMenuListByRoleId(roleId);
                if (menuList != null) {
                    for (Menu menu : menuList) {
                        List<Menu> list = menuDao.findMenuListByMenuId(menu.getId());
                        menu.setChildren(list);
                        menuListFin.add(menu);
                    }
                }
            }
        }
        return menuListFin;
      /*  User user = this.findByUsername(username);
        Set<Role> roles = user.getRoles();
        Set<Menu> menuList =new LinkedHashSet<>();
        for (Role role : roles) {
            LinkedHashSet<Menu> menuLinkedHashSet = role.getMenus();
            for (Menu menu : menuLinkedHashSet) {
                menuList.add(menu);
            }
        }
        return menuList;*/
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<User> page = userDao.pageQuery(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void deleteByIds(String ids) {
        String[] perIds = ids.split(",");
        for (String perId : perIds) {
            int id = Integer.parseInt(perId);
            userDao.deleteRelationMemberAndRole(id);
            userDao.deleteById(id);
        }
    }

    @Override
    public void add(User user, Integer[] roleIds) {
        //密码加密
        String password = user.getPassword();
        String encodePassword = bCryptPasswordEncoder.encode(password);
        user.setPassword(encodePassword);
        userDao.add(user);
        if (roleIds != null && roleIds.length > 0) {
            for (Integer roleId : roleIds) {
                Map map = new HashMap();
                map.put("user_id", user.getId());
                map.put("role_id", roleId);
                userDao.addRelationUserAndRole(map);
            }
        }
    }

    @Override
    public Integer[] findRoleIdsByUserId(Integer id) {
        return userDao.findRoleIdsByUserId(id);
    }

    @Override
    public void update(User user, Integer[] roleIds) {
        userDao.deleteRelationMemberAndRole(user.getId());
        userDao.update(user);
        if (roleIds != null && roleIds.length > 0) {
            for (Integer roleId : roleIds) {
                Map map = new HashMap();
                map.put("user_id", user.getId());
                map.put("role_id", roleId);
                userDao.addRelationUserAndRole(map);
            }
        }
    }

    @Override
    public void delete(Integer id) {
        userDao.deleteRelationMemberAndRole(id);
        userDao.deleteById(id);
    }
}

